/*
 * 网格类
 * */
import { Node, NodeType } from "./Node";
export class GridData {
    /** 列数 */
    public cols: number;
    /** 行数 */
    public rows: number;

    public nodes: Node[][] = [];

    /** 起始节点 */
    public startNode: Node;

    /** 终点节点 */
    public endNode: Node;

    constructor(cols: number, rows: number) {
        this.cols = cols;
        this.rows = rows;

        /*
         * 0 1列 2列  cols
         * 1 行
         * 2 行
         * rows
         * */

        //创建所有的节点
        for (let i = 0; i < this.cols; i++) {
            this.nodes[i] = [];
            for (let j = 0; j < this.rows; j++) {
                this.nodes[i][j] = new Node(i, j);
                this.nodes[i][j].walkable = Math.random() < 0.8;
            }
        }
    }

    //遍历所有子元素，将其设置会默认状态
    public setNormalStatus() {
        for (let i = 0; i < this.cols; i++) {
            for (let j = 0; j < this.rows; j++) {
                this.nodes[i][j].status = NodeType.NORMAL;
            }
        }
    }

    //获取节点
    public getNode(x: number, y: number) {
        return this.nodes[x][y];
    }

    //设置起始节点
    public setStartNode(x: number, y: number) {
        this.startNode = this.nodes[x][y];
        this.startNode.walkable = true;
    }

    //设置终点节点
    public setEndNode(x: number, y: number) {
        this.endNode = this.nodes[x][y];
        this.endNode.walkable = true;
    }

    //设置节点是否可行走
    public setWalkable(x: number, y: number, flag: boolean) {
        this.nodes[x][y].walkable = flag;
    }
}
